// 这份代码专门处理逻辑层的操作
const axios = require('axios');
const FormData = require('form-data');

const wxCheck = require('../util/wx_check');
const userDao = require('../dao/userDao');

const Response = require('../entity/http/Response');

const userService = {
    /**
     * @参数 头像id
     */
    async getAvater(id) {
        let result;
        try {
            result = await userDao.getAvater(id);
            result = (new Response()).ok("操作成功", result);
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    /**
     * @参数 用户id 
     */
    async getUserInfo(id) {
        let result;
        try {
            result = await userDao.getUserInfo(id);
            if (!result.length) {
                result = (new Response()).fail(404, "用户不存在");
            } else {
                result = (new Response()).ok("操作成功", result);
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    /**
     * 修改用户信息
     */
    async setUserInfo(uid, userInfo) {
        let result;
        try {
            // 调用微信审核接口进行审核
            let access_token = await wxCheck.getAccessToken();
            let text = JSON.stringify(userInfo);
            const { data } = await axios.post(`https://api.weixin.qq.com/wxa/msg_sec_check?access_token=${access_token}`,
                { content: text },
                { headers: { 'Content-Type': 'application/json' } }
            );
            if (data.errcode == 0) {
                // 调用 dao 层进行存储
                result = await userDao.setUserInfo(uid, userInfo);
                if (result.affectedRows) {
                    result = (new Response()).ok("操作成功");
                } else {
                    result = (new Response()).fail(404, "用户不存在");
                }
            } else {
                result = (new Response()).fail(500, "用户信息中包含非法内容");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    },
    async setAvatar(uid, avatar) {
        let result;
        try {
            // 调用微信审核接口进行审核
            let access_token = await wxCheck.getAccessToken();
            // 组装 form-data
            const form = new FormData();
            // const blob = new Blob([avatar.buffer], { type: 'image/jpeg' });
            form.append('media', avatar.buffer, {
                filename: 'check.jpg',   // 随便起，必须有后缀
                contentType: 'image/jpeg'
            });
            const { data } = await axios.post(`https://api.weixin.qq.com/wxa/img_sec_check?access_token=${ access_token }`,
                form,
                { headers: form.getHeaders() }
            );
            if (data.errcode == 0) {
                // 调用 dao 层进行存储
                result = await userDao.setAvatar(uid, avatar);
                if (result.affectedRows) {
                    result = (new Response()).ok("操作成功", { avatarId: result.avaterId });
                } else {
                    result = (new Response()).fail(500, "操作失败");
                }
            } else {
                result = (new Response()).fail(500, "图片包含非法内容");
            }
        } catch (err) {
            result = (new Response()).fail(500, "资源不可访问");
        }
        return result;
    }
};

module.exports = userService;